package crawler.web.utils;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import library.database.mongo.ObjectId;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import com.sleepycat.persist.StoreConfig;

import crawler.web.beans.PageFingerPrint;

public class FingerPrintDPL {
	class SimpleDA {
		// Index Accessors
		PrimaryIndex<ObjectId, PageFingerPrint> pIdx;
		SecondaryIndex<Long, ObjectId, PageFingerPrint> sIdx;

		// Open the indices
		public SimpleDA(EntityStore store) throws DatabaseException {
			// Primary key for SimpleEntityClass classes
			pIdx = store.getPrimaryIndex(ObjectId.class, PageFingerPrint.class);

			// Secondary key for SimpleEntityClass classes
			// Last field in the getSecondaryIndex() method must be
			// the name of a class member; in this case, an 
			// SimpleEntityClass.class data member.
			sIdx = store.getSecondaryIndex(pIdx, Long.class, "fingerPrint");
		}
	}

	public FingerPrintDPL(String dbPath) {
		envHome = new File(dbPath);
		setup();
	}

	//Next we create a method that simply opens our database environment and entity store for us.
	// The setup() method opens the environment and store
	// for us.
	public void setup() throws DatabaseException {
		EnvironmentConfig envConfig = new EnvironmentConfig();
		StoreConfig storeConfig = new StoreConfig();

		envConfig.setAllowCreate(true);
		storeConfig.setAllowCreate(true);

		if (!envHome.exists())
			envHome.mkdirs();
		// Open the environment and entity store
		envmnt = new Environment(envHome, envConfig);
		store = new EntityStore(envmnt, "EntityStore", storeConfig);
		sda = new SimpleDA(store);
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				shutdown();
			}
		});
	}

	//We also need a method to close our environment and store.
	// Close our environment and store.
	public void shutdown() throws DatabaseException {
		store.close();
		envmnt.close();
	}

	private File envHome;
	private Environment envmnt;
	private EntityStore store;
	private SimpleDA sda;

	public List<PageFingerPrint> getByFingerPrint(Long fp) {
		List<PageFingerPrint> list = new LinkedList<PageFingerPrint>();
		EntityCursor<PageFingerPrint> entities = sda.sIdx.subIndex(fp).entities();
		Iterator<PageFingerPrint> it = entities.iterator();
		while (it.hasNext())
			list.add(it.next());
		entities.close();
		return list;
	}

	//Now we need to create a method to actually write objects to our store. This method creates a SimpleDA object (see SimpleDA.class that we will use to access our indexes. Then we instantiate a series of CrawlURL (see A Simple Entity Class) objects that we will place in our store. Finally, we use our primary index (obtained from the SimpleDA class instance) to actually place these objects in our store.
	//In Retrieving Objects from an Entity Store we show a class that is used to retrieve these objects.
	// Populate the entity store
	public void save(PageFingerPrint pp) throws DatabaseException {
		// Instantiate and store some entity classes
		sda.pIdx.put(pp);
	}

	public void clear() {
		store.truncateClass(PageFingerPrint.class);
	}

	public long count() {
		return sda.pIdx.count();
	}
}
//
//
//
//
//
//
//
//
//
//